// Interpreter.h
/****************************************************/
#pragma once
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// 抽象表达式类
class Expression
{
   public:
    // 解释
    virtual int interpret() = 0;
};

// 数字表达式类
class NumberExpression : public Expression
{
   public:
    // 构造函数
    NumberExpression(int num) : number(num) {}

    // 解释
    virtual int interpret()
    {
        return number;
    }

   private:
    int number;
};

// 加法表达式类
class AddExpression : public Expression
{
   public:
    // 构造函数
    AddExpression(Expression *left, Expression *right) : left(left), right(right) {}

    // 解释
    virtual int interpret()
    {
        return left->interpret() + right->interpret();
    }

   private:
    Expression *left;
    Expression *right;
};

// 减法表达式类
class SubExpression : public Expression
{
   public:
    // 构造函数
    SubExpression(Expression *left, Expression *right) : left(left), right(right) {}

    // 解释
    virtual int interpret()
    {
        return left->interpret() - right->interpret();
    }

   private:
    Expression *left;
    Expression *right;
};

// 解释器类
class Interpreter
{
   public:
    // 构造函数
    Interpreter(string exp) : expression(exp) {}

    // 解释
    int interpret()
    {
        stack<Expression *> s;
        // 遍历表达式字符
        for (int i = 0; i < expression.length(); i++)
        {
            if (isdigit(expression[i]))
            {
                // 识别数字
                int j = i;
                while (j < expression.length() && isdigit(expression[j]))
                {
                    j++;
                }
                int num = stoi(expression.substr(i, j - i));
                s.push(new NumberExpression(num));
                i = j - 1;
            }
            else if (expression[i] == '+')
            {
                // 把左数提取出来
                Expression *left = s.top();
                s.pop();
                // 识别右数
                int j = i + 1;
                while (j < expression.length() && isdigit(expression[j]))
                {
                    j++;
                }
                Expression *right = new NumberExpression(stoi(expression.substr(i + 1, j - (i + 1))));
                // 左数+右数的表达式放入栈中
                s.push(new AddExpression(left, right));
                i = j - 1;
            }
            else if (expression[i] == '-')
            {
                // 把左数提取出来
                Expression *left = s.top();
                s.pop();
                // 识别右数
                int j = i + 1;
                while (j < expression.length() && isdigit(expression[j]))
                {
                    j++;
                }
                Expression *right = new NumberExpression(stoi(expression.substr(i + 1, j - (i + 1))));
                // 左数-右数的表达式放入栈中
                s.push(new SubExpression(left, right));
                i = j - 1;
            }
        }
        return s.top()->interpret();
    }

   private:
    string expression;
};